<link rel="stylesheet" href="/stylesheets/admin.css">
<style>
    .has-right {
        color: green;
    }
    .none-right {
        color: red;
    }
</style>
<div class="row">
    <div class="col-xs-3">
        <ul class="list-group">
            <li class="list-group-item list-group-title"><strong style="font-size: 16px">管理中心</strong></li>
            <% if (user && user.root) { %>
            <li class="list-group-item"><a class="menu-item" href="/admin/user">用户管理</a></li>
            <li class="list-group-item"><a class="menu-item" href="/admin/role">角色管理</a></li>
            <% } %>
            <li class="list-group-item"><a class="menu-item" href="/admin/robot">机器人管理</a></li>
            <li class="list-group-item"><a class="menu-item" href="/admin/delegater">代理机管理</a></li>
            <li class="list-group-item active"><a class="menu-item" href="/admin/gituser">Git用户管理</a></li>
            <% if (user && user.role && user.role.permissions.managerI) { %>
            <li class="list-group-item"><a class="menu-item" href="/admin/repo">Repo管理</a></li>
            <% } %>
        </ul>
    </div>
    <div class="col-xs-9">
        <div class="setting-div">
            <div class="setting-header-div">
                <button id="create-btn" class="create-btn btn btn-default">添加Git用户</button>
                <!--<span style="color: red">*&nbsp;此机器人为钉钉机器人，用于事件通知。</span>-->
                <hr>
            </div>
            <div>
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>名字</th>
                        <th>账号</th>
                        <th>密码</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="list-body">
                    </tbody>
                </table>
            </div>
            <div class="setting-footer-div">

            </div>
        </div>
        <div style="height: 40px"></div>
    </div>
</div>
<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">添加Git用户</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="name" class="col-sm-3 control-label">标识</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="name" placeholder="请输入标识">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="username" class="col-sm-3 control-label">账号</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="username" placeholder="请输入git仓库账号">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="passwd" class="col-sm-3 control-label">密码</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="passwd" placeholder="请输入git仓库密码">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="submit-btn" class="btn btn-primary">提交</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<link rel="stylesheet" href="/stylesheets/jquery-confirm.min-3.2.3.css">
<script src="/javascripts/jquery-confirm.min-3.1.1.js"></script>
<script language="JavaScript">

    var editId = null;

    $("#create-btn").click(function () {
        $('#myModalLabel').text('添加Git用户');
        $('#name').val('');
        $('#username').val('');
        $('#passwd').val('');
        $('#myModal').modal('show');
        editId = null;
    })

    function delClick(node) {
        $.confirm({
            title: '确定要删除该Git用户？',
            content: '删除后将无法恢复',
            animationBounce: 1.5,
            animation: 'rotateXR',
            closeAnimation: 'rotateYR',
            buttons: {
                ok: {
                    text: "确定",
                    btnClass: 'btn-success',
                    keys: ['enter'],
                    action: function(){
                        var tdNode = node.parentNode.parentNode;
                        var id = tdNode.id;
                        delAction(id)
                    }
                },
                cancel: {
                    text: "取消",
                    action:function(){
                    }
                }
            }
        });
    }

    function delAction(id) {
        $.post('/admin/gituser/del',{id:id},function (data) {
            if (data.code === 0) {
                showSuccess('已删除');
                loadGitUserList();
            } else {
                showError(data.message);
            }
        });
    }

    function editClick(node) {
        $('#myModalLabel').text('编辑Git用户');
        var tdNode = node.parentNode.parentNode;
        var id = tdNode.id;

        $('#name').val(tdNode.getElementsByTagName('td')[0].innerText);
        $('#username').val(tdNode.getElementsByTagName('td')[1].innerText);
        $('#passwd').val(tdNode.getElementsByTagName('td')[2].innerText);

        $('#myModal').modal('show');
        editId = id;
    }

    $("#submit-btn").click(function () {
        var name = $('#name').val();
        var username = $('#username').val();
        var passwd = $('#passwd').val();

        var param = {
            name : name,
            username : username,
            passwd : passwd,
            id : editId
        };
        if (name.trim().length == 0) {
            showError('标识不能为空');
            return;
        }
        if (username.trim().length == 0) {
            showError('账号不能为空');
            return;
        }
        if (passwd.trim().length == 0) {
            showError('密码不能为空');
            return;
        }
        if (editId) {
            $.post("/admin/gituser/edit",param, function(data) {
                if (data.code === 0) {
                    $('#myModal').modal('hide');
                    showSuccess('已更新Git用户 ' + name);
                    loadGitUserList();
                } else  {
                    showError(data.message);
                }
            });
        } else  {
            $.post("/admin/gituser/add",param, function(data) {
                if (data.code === 0) {
                    $('#myModal').modal('hide');
                    showSuccess('已添加Git用户 ' + name);
                    loadGitUserList();
                } else  {
                    showError(data.message);
                }
            });
        }
    })

    $(function () {
        loadGitUserList();
    })

    function loadGitUserList() {
        $.post("/admin/gituser/list",function (data) {
            if (data.code == 0) {
                var parent = document.getElementById('list-body');
                for (var i = parent.children.length - 1; i >= 0; i--) {
                    parent.removeChild(parent.children[i]);
                }
                for (var i = 0 ; i < data.data.length ; i++) {
                    var gituser = data.data[i];
                    var gituserNode = document.createElement('tr');
                    var id = gituser._id;

                    gituserNode.id = id;

                    var nameNode = document.createElement('td');
                    nameNode.id = 'name-' + id;
                    nameNode.innerText = gituser.name;

                    var usernameNode = document.createElement('td');
                    usernameNode.id = 'username-' + id;
                    usernameNode.innerText = gituser.username;

                    var passwdNode = document.createElement('td');
                    passwdNode.id = 'passwd-' + id;
                    passwdNode.innerText = gituser.passwd;

                    var  editNode = document.createElement('td');
                    editNode.innerHTML = "<a onclick='editClick(this)'>编辑</a>&nbsp;&nbsp;<a onclick='delClick(this)'>删除</a>";

                    gituserNode.appendChild(nameNode);
                    gituserNode.appendChild(usernameNode);
                    gituserNode.appendChild(passwdNode);
                    gituserNode.appendChild(editNode);

                    parent.appendChild(gituserNode);
                }

            } else  {

            }
        })
    }
</script>